package com.angel.bo.message.component.rocketmq.listener;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.angel.bo.message.component.rocketmq.ALiMqComponent;
import com.xiaoleilu.hutool.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhangwei
 * @date 2018/10/8 15:23
 * @desc 自定义阿里mq事件监听
 * 注:这里可以通过@Component生成单例示例,也可以通过new对象的方式注入
 */
@Component
public class MyOnsMqListener implements MessageListener {

    @Autowired
    private ALiMqComponent aLiMqComponent;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        try {
//            String someMsg = ObjectUtil.unserialize(message.getBody());
//            System.out.println(someMsg);
//            System.out.println(message.getTag());
//            System.out.println(message.getMsgID());
//            System.out.println(message.getTopic());
            // TODO: 2018/10/17  接收消息并处理
            return Action.CommitMessage;
        } catch (Exception e) {
            return aLiMqComponent.mqConsumerError(message, e);
        }
    }
}
